package com.moji.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moji.common.base.ApiResult;
import com.moji.entity.MjProductDetail;
import com.moji.entity.vo.CommonProductDetailDTO;
import com.moji.entity.vo.MjProductDetailVo;
import com.moji.service.MjProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.util.TextUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品详情 前端控制器
 * </p>
 *
 * @author myg
 * @since 2024-07-23
 */
@RestController
@RequestMapping("/api/mjProductDetail")
@Api(tags = "产品购买相关api接口", value = "产品购买相关api接口")
public class MjProductDetailController {
    /**
     * 服务对象
     */
    @Resource
    private MjProductDetailService mjProductDetailService;

    /**
     * 查询产品描述
     *
     * @param
     * @return
     */

    @PostMapping("/queryDetailByProductIDType")
    @ApiOperation(value = "查询产品描述,返回不同类型的描述，只传productId")
    public ApiResult<List<MjProductDetailVo>> queryDetailByProductIDType1(@RequestBody CommonProductDetailDTO commonProductDetailDTO) {
        //根据产品id返回不同类型的数据
        List<MjProductDetailVo> mjProductDetailVoList = new ArrayList<>();
        LambdaQueryWrapper<MjProductDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjProductDetail::getProductId, commonProductDetailDTO.getProductId());
        queryWrapper.select(MjProductDetail::getProductType).groupBy(MjProductDetail::getProductType);
        List<MjProductDetail> list = mjProductDetailService.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            MjProductDetailVo mjProductDetailVo = new MjProductDetailVo();
            LambdaQueryWrapper<MjProductDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(MjProductDetail::getProductType, list.get(i).getProductType());
            List<MjProductDetail> list1 = mjProductDetailService.list(queryWrapper1);
            if (list1 != null&&list1.size()>0) {
                mjProductDetailVo.setProductId(list1.get(0).getProductId());
                mjProductDetailVo.setTypeName(list1.get(0).getTypeName());
                mjProductDetailVo.setProductType(list1.get(0).getProductType());
                mjProductDetailVo.setDecrible(list1.get(0).getDecrible());
                mjProductDetailVo.setProductDuration(list1.get(0).getProductDuration());
                mjProductDetailVo.setProductPreferential(list1.get(0).getProductPreferential());
                mjProductDetailVo.setProductDuration(list1.get(0).getProductDuration());
            }
            mjProductDetailVoList.add(mjProductDetailVo);

        }
        return ApiResult.ok(mjProductDetailVoList);
    }
    /**
     * 查询产品描述
     *
     * @param
     * @return
     */

    @PostMapping("/queryDetailByProductIDORType")
    @ApiOperation(value = "根据产品id和产品类型查询产品描述,参数都必传")
    public ApiResult<MjProductDetailVo> queryDetailByProductIDType(@RequestBody CommonProductDetailDTO commonProductDetailDTO) {
        //根据产品id返回不同类型的数据
        LambdaQueryWrapper<MjProductDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjProductDetail::getProductId, commonProductDetailDTO.getProductId());
        queryWrapper.eq(MjProductDetail::getProductType, commonProductDetailDTO.getProductType());
        List<MjProductDetail> list = mjProductDetailService.list(queryWrapper);
        MjProductDetailVo mjProductDetailVo=new MjProductDetailVo();
        if (list != null&&list.size()>0) {
            mjProductDetailVo.setProductId(list.get(0).getProductId());
            mjProductDetailVo.setTypeName(list.get(0).getTypeName());
            mjProductDetailVo.setProductType(list.get(0).getProductType());
            mjProductDetailVo.setDecrible(list.get(0).getDecrible());
            mjProductDetailVo.setProductDuration(list.get(0).getProductDuration());
            mjProductDetailVo.setProductPreferential(list.get(0).getProductPreferential());
            mjProductDetailVo.setProductDuration(list.get(0).getProductDuration());
        }


        return ApiResult.ok(mjProductDetailVo);
    }

    /**
     * 修改产品描述
     *
     * @param
     * @return
     */
    @PostMapping("/updateByProductIDType")
    @ApiOperation(value = "修改产品描述，需要传productId和productType")
    public ApiResult<Boolean> queryDetailByProductIDType(@RequestBody MjProductDetailVo mjProductDetailVo) {
        CommonProductDetailDTO commonProductDetailDTO = new CommonProductDetailDTO();
        commonProductDetailDTO.setProductType(mjProductDetailVo.getProductType());
        commonProductDetailDTO.setProductId(mjProductDetailVo.getProductId());
        List<MjProductDetail> mjBannerMyPage = mjProductDetailService.queryDetailByProductID(commonProductDetailDTO);
        for (int i = 0; i < mjBannerMyPage.size(); i++) {
            MjProductDetail mjProductDetail = mjBannerMyPage.get(i);
            if (!TextUtils.isEmpty(mjProductDetailVo.getDecrible())) {
                mjProductDetail.setDecrible(mjProductDetailVo.getDecrible());
            }

            if (!TextUtils.isEmpty(mjProductDetailVo.getTypeName())) {
                mjProductDetail.setTypeName(mjProductDetailVo.getTypeName());
            }
            if (!TextUtils.isEmpty(mjProductDetailVo.getProductDuration())) {
                mjProductDetail.setProductDuration(mjProductDetailVo.getProductDuration());
            }
            if (!TextUtils.isEmpty(mjProductDetailVo.getProductPreferential())) {
                mjProductDetail.setProductPreferential(mjProductDetailVo.getProductPreferential());
            }
            if (!TextUtils.isEmpty(mjProductDetailVo.getProductDuration())) {
                mjProductDetail.setProductDuration(mjProductDetailVo.getProductDuration());
            }
            mjProductDetailService.update(mjProductDetail);
        }


        return ApiResult.ok(Boolean.TRUE);
    }

    /**
     * 查询产品详情数据
     *
     * @param
     * @return
     */
    @PostMapping("/queryDetailByProductID")
    @ApiOperation(value = "根据productId产品属性相关数据,productType非必传")
    public ApiResult<List<MjProductDetail>> queryDetailByProductID(@RequestBody CommonProductDetailDTO commonProductDetailDTO) {
        List<MjProductDetail> mjBannerMyPage = mjProductDetailService.queryDetailByProductID(commonProductDetailDTO);
        return ApiResult.ok(mjBannerMyPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/detail")
    @ApiOperation(value = "根据id查看产品属性", response = MjProductDetail.class)
    public ApiResult<MjProductDetail> queryById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjProductDetailService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param mjSolution 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加产品属性", response = MjProductDetail.class)
    public ApiResult<MjProductDetail> add(@Validated @RequestBody MjProductDetail mjSolution) {
        return ApiResult.ok(this.mjProductDetailService.insert(mjSolution));
    }

    /**
     * 编辑数据
     *
     * @param mjSolution 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改产品属性", response = MjProductDetail.class)
    public ApiResult<MjProductDetail> edit(@Validated @RequestBody MjProductDetail mjSolution) {
        return ApiResult.ok(this.mjProductDetailService.update(mjSolution));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除产品属性", response = Boolean.class)
    public ApiResult<Boolean> deleteById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjProductDetailService.deleteById(id));
    }
}

